<?php
namespace app\channel\controller;
use think\Db;

class DataChannel extends Base
{
    public function index(){
        if(request()->isAjax() || input('?export')){
            $plat = input('plat');
            $date = explodeTime(input('date'));
            $channelId = input('channel_id');

            if($plat=='cps'){
                !session('admin.cps_id') ? $this->layTable(0, '', [], 0) : $id=session('admin.cps_id');
            }else{
                !session('admin.sdk_id') ? $this->layTable(0, '', [], 0) : $id=session('admin.sdk_id');
            }

            if( strtotime(date('Y-m-d', $date['end_time'])) == strtotime(date('Y-m-d', time())) ){
                $selectChannel = model('manage/Data')->selectChannelNow($plat, $date['start_time'], $date['end_time'], $channelId, $id);
            }else{
                $selectChannel = model('manage/Data')->selectChannelOld($plat, $date['start_time'], $date['end_time'], $channelId, $id);
            }
            if(input('?export')){
                array_unshift($selectChannel['data'], ['统计日期', '渠道ID', '渠道名称', '平台新增', '游戏新增', '游戏创角', '游戏活跃', '付费人数', '付费金额', '付费率', '新增用户ARPU', '活跃用户ARPU', '付费用户ARPU']);
                exportExcel($selectChannel['data'], input('date').'渠道统计', input('date').'渠道统计');
            }

            $this->layTable(0, '渠道统计', $selectChannel['data'], $selectChannel['count'], $selectChannel['sum']);
        }else{
            $date = date('Y-m-d', time()-86400).' - '.date('Y-m-d', time()-86400);
            $today = date('Y-m-d', time()).' - '.date('Y-m-d', time());
            $channelData = model('Base')->channelNotice();

            if(session('admin.cps_id') && session('admin.sdk_id')){
                $cpsSelected = 'selected';
                $sdkSelected = '';
            }else if(session('admin.cps_id')){
                $cpsSelected = 'selected';
                $sdkSelected = '';
            }else if(session('admin.sdk_id')){
                $cpsSelected = '';
                $sdkSelected = 'selected';
            }
            return view('', ['date'=>$date, 'today'=>$today, 'channel_data'=>$channelData, 'cps_selected'=>$cpsSelected, 'sdk_selected'=>$sdkSelected]);
        }
    }

    public function chart(){
        $plat = input('plat');
        $date = explodeTime(input('daytime'));
        $channelId = input('channel_id');

        if($plat=='cps'){
            $id = session('admin.cps_id');
        }else{
            $id = session('admin.sdk_id');
        }

        //24小时内
        if( date('Y-m-d', $date['start_time']) == date('Y-m-d', $date['end_time']) ){
            if( date('Y-m-d', $date['end_time'])==date('Y-m-d', time()) ){
                $time = date('H', time())+1;
                for($i=1; $i<=$time; $i++){
                    $d = $i < 10 ? 0 . $i : $i;
                    $dateType[] = $d.':00';
                }
            }else{
                $time = 24;
                $dateType = ['01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00', '24:00'];
            }
            $findChannel = model('manage/Data')->findChannelNow($plat, $date['start_time'], $date['end_time'], $channelId, $time, $id);

            //如果小于31天，则按天分组
        }else if( ($date['end_time']-$date['start_time']) < 86400*31 ){
            $findChannel = model('manage/Data')->findChannelOld($plat, $date['start_time'], $date['end_time'], $channelId, '%m-%d', $id);

            for ($start = $date['start_time']; $start <= $date['end_time']; $start += 24 * 3600) {
                $dateType[] = date('m-d',$start);
            }
            //如果大于31天，按月分组
        }else{
            $findChannel = model('manage/Data')->findChannelOld($plat, $date['start_time'], $date['end_time'], $channelId, '%Y-%m', $id);

            $i = false;
            $start = $date['start_time'];
            while( $start < strtotime('-1 Month', $date['end_time']) ) {
                $start = !$i ? strtotime('+0 Month', $start) : strtotime('+1 Month', $start);
                $i = true;
                $dateType[] = date('Y-m', $start);
            }
        }

        return view('', ['series'=>$findChannel, 'date_type'=>json_encode($dateType)]);
    }
}